package com.xuexiang.xuidemo.provider;

import com.xuexiang.xuidemo.ResourceTable;

import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 页面切换迭代器
 *
 * @since 2021-04-22
 */
public class PageProvider extends PageSliderProvider {

    LayoutScatter layoutScatter;
    private ArrayList<Component> components = new ArrayList<>();
    private int[] recourceImage = new int[]{
        ResourceTable.Media_guide_img_1, ResourceTable.Media_guide_img_2, ResourceTable.Media_guide_img_3, ResourceTable.Media_guide_img_4,
    };
    private Context context;

    /**
     * 构造方法
     *
     * @param context
     */
    public PageProvider(Context context) {
        layoutScatter = LayoutScatter.getInstance(context);
        this.context = context;
    }

    /**
     * 获取个数
     *
     * @return return
     */
    @Override
    public int getCount() {
        return recourceImage.length;
    }

    /**
     * 打印输出日志
     */
    public static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MainAbilitySlice");

    /**
     * 获取Child 没有
     *
     * @param componentContainer componentContainer
     * @param position position
     * @return return
     */
    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int position) {
        Component page1 = layoutScatter.parse(ResourceTable.Layout_page_slide_layout, null, false);
        Text tv = (Text) page1.findComponentById(ResourceTable.Id_tv);
        try {
            PixelMapElement pixelMapElement = new PixelMapElement(context.getResourceManager().getResource(recourceImage[position]));
            page1.setBackground(pixelMapElement);
            tv.setText("page" + position);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        }
        componentContainer.addComponent(page1);
        components.add(position, page1);
        return page1;
    }

    /**
     * 销毁页面切换的对应页面
     *
     * @param componentContainer
     * @param i
     * @param o
     */
    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return component == o;
    }

    /**
     * 获取对应的页面组件
     *
     * @param index
     * @return 组件
     */
    public Component getComponent(int index) {
        if (index < 0 || index > components.size()) {
            return null;
        }
        return components.get(index);
    }

    /**
     * 获取页面组件数组
     *
     * @return 页面组件数组
     */
    public List<Component> getComponents() {
        return components;
    }

}

